//
//  HomeViewController.m
//  sentorise
//
//  Created by admin on 2025/8/22.
//

#import "HomeViewController.h"
#import "GlobalDefine.h"
#import "DeviceViewController.h"
#import "MoreViewController.h"
#import "SystemViewController.h"
#import "BTManager.h"

@interface HomeViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomSpace;
@property (strong, nonatomic) UIScrollView *sv;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tipsW;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tipsTop;
@property (weak, nonatomic) IBOutlet UIScrollView *tipsSV;

@property (weak, nonatomic) IBOutlet UIImageView *deviceIV;
@property (weak, nonatomic) IBOutlet UIImageView *deviceSeg;
@property (weak, nonatomic) IBOutlet UIImageView *systemIV;
@property (weak, nonatomic) IBOutlet UIImageView *systemSeg;
@property (weak, nonatomic) IBOutlet UIImageView *moreIV;
@property (weak, nonatomic) IBOutlet UIImageView *moreSeg;

@property (weak, nonatomic) IBOutlet UILabel *segLabel1;
@property (weak, nonatomic) IBOutlet UILabel *segLabel2;
@property (weak, nonatomic) IBOutlet UILabel *segLabel3;


@property (strong, nonatomic) DeviceViewController *vc1;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    self.bottomSpace.constant = IphoneX ? 34 : 0;
    self.tipsTop.constant = IphoneX ? 32 : 20;
    self.tipsW.constant = kWidth;
    
    CGFloat height = kHeight - 54 - (IphoneX ? 34 : 0);
    self.sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, height)];
    self.sv.bounces = NO;
    self.sv.pagingEnabled = YES;
    self.sv.showsHorizontalScrollIndicator = NO;
    self.sv.delegate = self;
    self.sv.scrollEnabled = NO;
    self.sv.contentSize = CGSizeMake(kWidth * 3, height);
    [self.view addSubview:self.sv];
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth*3, height)];
    contentView.backgroundColor = [UIColor blueColor];
    [self.sv addSubview:contentView];

    self.vc1 = [[DeviceViewController alloc] initWithNibName:@"DeviceViewController" bundle:nil];
    self.vc1.nv = self.navigationController;
    [self addChildViewController:self.vc1];
    [contentView addSubview:self.vc1.view];
    self.vc1.view.frame = CGRectMake(0, 0, kWidth, height);
    
    MoreViewController *vc2 = [[MoreViewController alloc] initWithNibName:@"MoreViewController" bundle:nil];
    vc2.nv = self.navigationController;
    [self addChildViewController:vc2];
    [contentView addSubview:vc2.view];
    vc2.view.frame = CGRectMake(kWidth, 0, kWidth, height);
    
    SystemViewController *vc3 = [[SystemViewController alloc] initWithNibName:@"SystemViewController" bundle:nil];
    vc3.nv = self.navigationController;
    [self addChildViewController:vc3];
    [contentView addSubview:vc3.view];
    vc3.view.frame = CGRectMake(kWidth * 2, 0, kWidth, height);
    
    [self.view bringSubviewToFront:self.tipsSV];
    
    NSInteger isFirst = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstIn"] integerValue];
    self.tipsSV.hidden = isFirst == 1;
    [[NSUserDefaults standardUserDefaults] setObject:@(1) forKey:@"isFirstIn"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    self.segLabel1.text = NSLocalizedString(@"Device", nil);
    self.segLabel2.text = NSLocalizedString(@"System", nil);
    self.segLabel3.text = NSLocalizedString(@"More", nil);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (!self.deviceSeg.hidden && self.navigationController.viewControllers.count == 1) {
        self.vc1.isCurrentPage = YES;
    }
}

- (IBAction)actionTipsClose:(id)sender {
    self.tipsSV.hidden = YES;
}

- (IBAction)actionDevice:(id)sender {
    self.deviceIV.image = [UIImage imageNamed:@"tab_1_s"];
    self.systemIV.image = [UIImage imageNamed:@"tab_2_n"];
    self.moreIV.image = [UIImage imageNamed:@"tab_3_n"];
    self.deviceSeg.hidden = NO;
    self.systemSeg.hidden = YES;
    self.moreSeg.hidden = YES;
    self.sv.contentOffset = CGPointMake(0, 0);
    self.vc1.isCurrentPage = YES;
    if (![BTManager sharedBTManager].isGettingBattery) {
        [self.vc1 searchDevices];
        if (![BTManager sharedBTManager].isGettingBattery) {
            [BTManager sharedBTManager].isGettingBattery = YES;
        }
    }
}

- (IBAction)actionSystem:(id)sender {
    self.deviceIV.image = [UIImage imageNamed:@"tab_1_n"];
    self.systemIV.image = [UIImage imageNamed:@"tab_2_s"];
    self.moreIV.image = [UIImage imageNamed:@"tab_3_n"];
    self.deviceSeg.hidden = YES;
    self.systemSeg.hidden = NO;
    self.moreSeg.hidden = YES;
    self.sv.contentOffset = CGPointMake(kWidth, 0);
    self.vc1.isCurrentPage = NO;
    [BTManager sharedBTManager].isGettingBattery = NO;
}

- (IBAction)actionMore:(id)sender {
    self.deviceIV.image = [UIImage imageNamed:@"tab_1_n"];
    self.systemIV.image = [UIImage imageNamed:@"tab_2_n"];
    self.moreIV.image = [UIImage imageNamed:@"tab_3_s"];
    self.deviceSeg.hidden = YES;
    self.systemSeg.hidden = YES;
    self.moreSeg.hidden = NO;
    self.sv.contentOffset = CGPointMake(kWidth * 2, 0);
    self.vc1.isCurrentPage = NO;
    [BTManager sharedBTManager].isGettingBattery = NO;
}


@end
